package top.jacktgq.sxt.bio.tcp.demo12;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author CandyWall
 * @Date 2019/10/21--14:21
 * @Description 模拟登录 多个客户端请求：验证客户端的账号密码是否正确，并且给出响应
 *                 服务器端：
 *                  1、指定端口 使用ServerSocket创建服务器
 *                  2、阻塞式等待连接 accept
 *                  3、操作：输入输出流
 *                  4、释放资源
 */
public class TCPMultiLoginServer {
    public static void main(String[] args) throws IOException {
        //1、指定端口 使用ServerSocket创建服务器
        ServerSocket serverSocket = new ServerSocket(8888);
        while (true) {
            //2、阻塞式等待连接 accept
            Socket server = serverSocket.accept();
            System.out.println("一个客户端建立了连接！");
            new Thread(new Channel(server)).start();
        }
        //serverSocket.close();
    }

    static class Channel implements Runnable {
        private Socket server;
        private DataInputStream dis;
        private DataOutputStream dos;
        public Channel(Socket server) {
            this.server = server;
            //获取输入流
            try {
                dis = new DataInputStream(server.getInputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
            //获取输出流
            try {
                dos = new DataOutputStream(server.getOutputStream());
                dos.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void run() {
            while (true) {
                //3、操作：输入输出流

                //将获取到的登录参数进行解析
                String username = null;
                String password = null;
                //先根据&符号进行分割
                String[] strs = receive().split("&");
                for (String str : strs) {
                    //再根据=号进行分割
                    String[] params = str.split("=");
                    if (params[0].equals("username")) {
                        username = params[1];
                    } else if (params[0].equals("password")) {
                        password = params[1];
                    }
                }
                System.out.println("username:" + username + ",password:" + password);
                //客户端密码是否正确
                String result = "密码错误";
                if ("candywall".equals(username) && "123456".equals(password)) {
                    result = "密码正确";
                }
                //将结果写给客户端
                send(result);

                if (result.equals("密码正确")) {
                    break;
                }
            }
            //4、释放资源
            release();
        }

        //释放资源
        private void release() {
            try {
                if (dis != null) {
                    dis.close();
                }
                if (dos != null) {
                    dos.close();
                }
                if (server != null) {
                    server.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //发送信息
        private void send(String msg) {
            try {
                dos.writeUTF(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //接收信息
        private String receive() {
            String content = "";
            try {
                content = dis.readUTF();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return content;
        }

    }



}
